package com.itheima.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


/*    @GetMapping("/category/findAll")//查询所有分类
    public ResultInfo findAll()
    {
        List<Category> categories = categoryService.findAll();
        return ResultInfo.success(categories);
    }*/
    @PostMapping("/category")//添加分类
    public ResultInfo add(@RequestBody Category category)
    {
        categoryService.save(category);
        return ResultInfo.success(null);
    }
    @PutMapping("/category")//修改分类
    public ResultInfo edit(@RequestBody Category category)
    {
        categoryService.update(category);
        return ResultInfo.success(null);
    }
    @DeleteMapping("/category")//删除分类
    public ResultInfo delete(Long id)
    {
       ResultInfo resultInfo= categoryService.deleteByid(id);
        return resultInfo;
    }

    @GetMapping("/category/findAll")//查询所有分类
    public ResultInfo findPage(@RequestParam(value="pagenum",defaultValue = "1") Integer pageNum,@RequestParam(value="pagesize",defaultValue = "3") Integer pageSize)
    {
        List<Category> categories = categoryService.findPage(pageNum,pageSize);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);
        return ResultInfo.success(pageInfo);
    }
}
